/*
 * ShopPresenter.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shop;

import com.yidiandao.business.data.model.IShopModel;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.List;

/** 首页-商铺-Presenter */
public class ShopPresenter implements ShopContract.Presenter {

    private ShopContract.View mShopView;

    private IShopModel mShopModel;

    public ShopPresenter(ShopContract.View view, IShopModel shopModel) {
        mShopView = view;
        mShopModel = shopModel;

        mShopView.setPresenter(this);
    }

    @Override
    public void start() {
        loadShopList(true);
    }

    @Override
    public void loadShopList(final boolean isRefreshing) {

        mShopModel.loadHomeShopList(
                isRefreshing,
                mShopView.getPagerTag(),
                new IShopModel.OnLoadShopsListener() {
                    @Override
                    public void onSuccess(
                            List<DisplayListItem> displayListItems,
                            boolean emptyData,
                            boolean hasMoreData) {

                        if (isRefreshing) {
                            mShopView.refreshContent(mShopView.checkResultObject(displayListItems));
                            mShopView.setRefreshComplete();
                        }

                        mShopView.loadMoreFinish(emptyData, hasMoreData);
                        mShopView.setDisplayListItems(displayListItems);
                    }

                    @Override
                    public void onFailed() {
                        if (isRefreshing) {
                            mShopView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                        } else {
                            mShopView.loadMoreError();
                        }
                    }
                });
    }
}
